<template>
  <div class="wechat-bind">
    <h1>添加文章</h1>

    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item label="标题" prop="title">
        <el-input v-model="ruleForm.title"></el-input>
      </el-form-item>
      <el-form-item label="关键词" prop="keyword">
        <el-input type="textarea" v-model="ruleForm.keyword"></el-input>
      </el-form-item>
      <el-form-item label="描述" prop="description">
        <el-input type="textarea" v-model="ruleForm.description"></el-input>
      </el-form-item>
      <el-form-item label="内容" prop="content">
        <vue-editor v-model="ruleForm.content"></vue-editor>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">保存修改</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
    import { VueEditor } from 'vue2-editor'

    export default {
        name:'ArticleAdd',
        components: {
            VueEditor
        },
        data() {
            return {
                apiUrl:apiUrl,
                ruleForm: {
                    title: '',
                    keyword: '',
                    description: '',
                    content: '',
                },
                rules: {
                    title: [
                        { required: true, message: '请输入标题', trigger: 'blur' },
                    ],
                    content: [
                        { required: true, message: '请输入内容', trigger: 'blur' }
                    ]
                }
            };
        },
        mounted () {
            this.$nextTick(function () {
                this.load(this.$route.params.id);//概况
            })
        },
        computed:{
            editUrl(){
                return this.apiUrl+'Article/edit';
            },
            detailUrl(){
                return this.apiUrl+'/Article/detail';
            }
        },
        methods: {
            //概况数据获取
            load(id) {
                  this.$http.get(this.detailUrl,{
                      params: { id: id }
                  })
                  .then(function (response) {
                      this.ruleForm = response.data;
                  })
                  .catch(function (response) {
                    console.log(response);
                  });
            },
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        this.$http.post(this.editUrl, this.ruleForm).then(response => {
                            if(response.data.status==1){
                            this.$message.success('保存成功');
                            this.$router.push('/article/list/1');
                        }else{
                            this.$message.error('保存失败');
                        }
                    }, response => {
                            this.$message.error('保存失败');
                            console.log(response);
                        });
                    } else {
                        console.log('error submit!!');
                return false;
            }
            });
            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
            }
        }
    }
</script>


<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>

</style>
